﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Nsf.Samples.RetryActivitySample.Infrastructure.Common.FaultDetails;

namespace Nsf.Samples.RetryActivitySample.Infrastructure.ServiceBase
{
    public abstract class ServiceBase
    {

        static ServiceBase()
        {
            ExceptionToFaultConverter.RegisterConverter<Exception, ProcessingFaultDetails>(
                ex =>
                new ProcessingFaultDetails() { Message = "Внутренняя ошибка сервера", Details = ex.Message });
        }

        protected virtual void ThrowParameterValidationException(String reasonParameter, String errorCode, String errorDescription, params Object[] descriptionParams)
        {
            throw new FaultException<ValidationFaultDetails>(
                new ValidationFaultDetails(errorCode, String.Format(errorDescription, descriptionParams)),
                String.Format("Неверный параметр '{0}'", reasonParameter));
        }

        protected virtual void ThrowObjectValidationException(String reason, List<ValidationFaultDetailsItem> errors)
        {
            if (errors != null && errors.Count > 0)
            {
                throw new FaultException<ValidationFaultDetails>(new ValidationFaultDetails(errors), reason);
            }
        }

        protected virtual void ThrowObjectValidationException(List<ValidationFaultDetailsItem> errors)
        {
            if (errors != null && errors.Count > 0)
            {
                throw new FaultException<ValidationFaultDetails>(new ValidationFaultDetails(errors), "Во время проверки объекта были найдены ошибки");
            }
        }

        protected virtual void ThrowObjectValidationException(String code, String description)
        {
            throw new FaultException<ValidationFaultDetails>(new ValidationFaultDetails(new List<ValidationFaultDetailsItem> { new ValidationFaultDetailsItem() { Code = code, Description = description } }));
        }

        protected virtual void ThrowProcessingErrorException(String message, String details)
        {
            throw new FaultException<ProcessingFaultDetails>(new ProcessingFaultDetails() { Message = message, Details = details });
        }
    }
}
